package mt.client.gui;

import javax.swing.table.AbstractTableModel;

import mt.ordersList.OrderList;
import mt.ordersList.OrdersManager;

/**
 * Class used to create a Table in which the orders will be stored
 * 
 * @author André Malvas
 * @author César Alves
 * @author Jorge Martins
 * @author Ricardo Silvestre
 */

public class TableModel extends AbstractTableModel {

	/**
	 * Number of columns
	 */
	private int columnsCount;

	/**
	 * List of orders
	 */
	private OrderList orderList;

	/**
	 * Constructor of the class
	 * 
	 * @param ordersManager
	 *            manager of the orders
	 * @param orderListType
	 *            type of the List passed to the table
	 */
	public TableModel(OrdersManager ordersManager, OrderList.Type orderListType) {
		this.columnsCount = 5;
		if (orderListType == OrderList.Type.MY_ORDER_LIST)
			orderList = ordersManager.getMyOrdersList();
		else if (orderListType == OrderList.Type.PUBLIC_ORDER_LIST)
			orderList = ordersManager.getPublicOrdersList();
	}

	/**
	 * Gets the number of columns
	 * 
	 * @return number of columns
	 */
	@Override
	public int getColumnCount() {
		return this.columnsCount;
	}

	/**
	 * Gets the number of rows
	 * 
	 * @return number of rows
	 */
	@Override
	public int getRowCount() {
		return orderList.getLength();
	}

	/**
	 * Gets the Order indicated by the row and col parameters
	 * 
	 * @param row
	 *            row to look for the Order
	 * @param col
	 *            column to look for the Order
	 * 
	 * @return The order
	 */
	@Override
	public Object getValueAt(int row, int col) {
		switch (col) {
		case 0:
			if (orderList.getOrder(row).isBuyOrder())
				return "Buy";
			else if (orderList.getOrder(row).isSellOrder())
				return "Sell";
		case 1:
			return orderList.getOrder(row).getNickname();
		case 2:
			return orderList.getOrder(row).getStock();
		case 3:
			return orderList.getOrder(row).getNumberOfUnits();
		case 4:
			return orderList.getOrder(row).getPricePerUnit();
		}
		return null;
	}

	/**
	 * Sets the name of the columns
	 * 
	 * @param column
	 *            number of the column
	 * @return The column name
	 */
	@Override
	public String getColumnName(int column) {
		switch (column) {
		case 0:
			return "Order Type";
		case 1:
			return "Nickname";
		case 2:
			return "Nome Ac�‹o";
		case 3:
			return "Quantidade";
		case 4:
			return "Pre�o";
		}
		return null;
	}

}
